/**   
* @Title: LocalIpAddressUtil.java 
* @Package: com.ifuntimes.utils
* @Description: LocalIpAddressUtil
* @author:YuHai
* @Email: it@ifuntimes.com  
* @date 2017年09月05日 下午4:02:21 
* @version V1.0   
*/
package com.ifuntimes.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName: LocalIpAddressUtil
 * @Description: LocalIpAddressUtil
 * @author YuHai it@ifuntimes.com
 * @date 2017年09月05日 下午4:02:21
 * 
 */
public class LocalIpAddressUtil {
	/**
	 * 获取本地ip地址，有可能会有多个地址, 若有多个网卡则会搜集多个网卡的ip地址
	 */
	public static Set<InetAddress> resolveLocalAddresses() {
		Set<InetAddress> addrs = new HashSet<InetAddress>();
		Enumeration<NetworkInterface> ns = null;
		try {
			ns = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			// ignored...
		}
		while (ns != null && ns.hasMoreElements()) {
			NetworkInterface n = ns.nextElement();
			Enumeration<InetAddress> is = n.getInetAddresses();
			while (is.hasMoreElements()) {
				InetAddress i = is.nextElement();
				if (!i.isLoopbackAddress() && !i.isLinkLocalAddress() && !i.isMulticastAddress()
						&& !isSpecialIp(i.getHostAddress()))
					addrs.add(i);
			}
		}
		return addrs;
	}

	public static Set<String> resolveLocalIps() {
		Set<InetAddress> addrs = resolveLocalAddresses();
		Set<String> ret = new HashSet<String>();
		for (InetAddress addr : addrs)
			ret.add(addr.getHostAddress());
		return ret;
	}

	private static boolean isSpecialIp(String ip) {
		if (ip.contains(":"))
			return true;
		if (ip.startsWith("127."))
			return true;
		if (ip.startsWith("169.254."))
			return true;
		if (ip.equals("255.255.255.255"))
			return true;
		return false;
	}
}
